package wc;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;

public class Main {
    public static String inputFile;
    public static String outputFile;
    public static boolean needC;
    public static boolean needW;
    public static boolean needL;
    public static boolean needO;
    public static void main(String[] args)
    {
        inputFile="C:\\Users\\wuhen\\Desktop\\1.txt";
        for(int i=0;i<args.length;i++)
        {
            System.out.println(args[i]);
            if ("-c".equals(args[i])) {
                needC = true;
            } else if ("-w".equals(args[i])) {
                needW = true;
            } else if ("-l".equals(args[i])) {
                needL = true;
            } else if ("-o".equals(args[i])) {
                needO = true;
                outputFile = args[i + 1];
            } else {
                if (!args[i - 1].equals("-e") && !args[i - 1].equals("-o")) {

                    inputFile = args[i];
                }
            }

        }
        String outputStr="";
        ArrayList<String> fileNames =new ArrayList<String>();
        fileNames.add(inputFile);
        int len=fileNames.size();
        String fn;

        for(int i=0;i<len;i++)
        {
            fn=fileNames.get(i);
            System.out.println(fn);
            String fileShortName=fn.substring(fn.lastIndexOf("\\")+1, fn.length());

            if(needC||needW||needL)
            {

                Wc wc= BasicInfo.basicInfo(fn);



                if(needC)
                {

                    outputStr+=fileShortName;
                    outputStr+=", char: ";
                    outputStr+=wc.getChars();
                    outputStr+="\r\n";
                }
                if(needW)
                {

                    outputStr+=fileShortName;
                    outputStr+=", word: ";
                    outputStr+=wc.getWords();
                    outputStr+="\r\n";
                }
                if(needL)
                {

                    outputStr+=fileShortName;
                    outputStr+=", line: ";
                    outputStr+=wc.getWords();
                    outputStr+="\r\n";
                }
            }

        }

        System.out.println(outputStr);
        if(!needO)
        {
            outputFile="result.txt";
        }
        try
        {

            File writename = new File( outputFile);
            writename.createNewFile();
            BufferedWriter out = new BufferedWriter(new FileWriter(writename));
            out.write(outputStr);
            out.flush();
            out.close();
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }


    }

}
